package demo1;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectStudent {
     public static void select (Connection connection , int id) throws SQLException {
         ResultSet resultSet = selectResultSet(connection, id);
         while (resultSet.next()){
             System.out.println( "id:"+resultSet.getInt("id") + " name:" + resultSet.getString("name"));
         }
     }

    private static ResultSet selectResultSet(Connection connection , int id) throws SQLException {
        if(id == -1){
            String sql = "select * from  student ";
            PreparedStatement statement = connection.prepareStatement(sql);
            return statement.executeQuery();
        }
        String sql = "select * from  student  where id=?";
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.setInt(1,id);
        return statement.executeQuery();
    }

}
